<?php
/**
 * 関数プラグイン
 *
 * @author tanaka
 * @package View_Smarty_Plugin
 * @subpackage Functions
 */
final class View_Smarty_Plugin_Functions extends Base_Singleton
{
	/**
	 * クラス定数呼び出し
	 *
	 * @param array $params
	 * @param Smarty $smarty
	 * @throws Exception
	 * @return mixed
	 */
	public function constant($params, &$smarty)
	{
		if (!isset($params['class']) || !isset($params['name'])) {
			throw new Exception('引数が不正');
		}

		$class = $params['class'];
		$name = $params['name'];

		if (!@class_exists($class, true)) {
			throw new Exception(sprintf('クラス【%s】は存在しない。', $class));
		}

		$reflector = new ReflectionClass($class);
		if (!$reflector->hasConstant($name)) {
			throw new Exception(sprintf('クラス【%s】に定数【%s】は存在しない。', $class, $name));
		}

		$result = $reflector->getConstant($name);
		
		if(isset($params['assign']) && strlen($params['assign'])){
			return $smarty->assign($params['assign'], $result);
		}
		return $result;
	}

	/**
	 * 静的メソッド呼び出し
	 *
	 * @param array $params
	 * @param Smarty $smarty
	 * @throws Exception
	 * @return mixed
	 */
	public function callStatic($params, &$smarty)
	{
		
		if (!isset($params['class']) || !isset($params['name'])) {
			throw new Exception('引数が不正');
		}

		$class = $params['class'];
		$name = $params['name'];

		if (!@class_exists($class, true)) {
			throw new Exception(sprintf('クラス【%s】は存在しない。', $class));
		}
		
		if(isset($params['arg'])){
			$result = $class::$name($params['arg']);
		}
		else{
			$result = $class::$name();
		}
		
		if(isset($params['assign']) && strlen($params['assign'])){
			return $smarty->assign($params['assign'], $result);
		}
		return $result;
	}
}
